﻿using Autofac;
using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Options;
using SqlSugar;
using WCS.Entities;
using WCS.IServices;
using WCS.Services;
using WCS.ViewModels;

namespace WCS.LongTaskService
{
    public class TaskAssignService : BackgroundService
    {

        #region  Fields

        private readonly ILogger<TaskAssignService> logger;
        private readonly IOptionsMonitor<SystemParameter> options;
        private readonly ILifetimeScope lifetimeScope;
        private readonly IHubContext<ReportHub> _hubContext;
        private readonly IMapper mapper;
        // private readonly IExecutePath excuteRoute;

        // private readonly ISqlSugarClient sqlSugarClient;
        #endregion

        public TaskAssignService(
            ILogger<TaskAssignService> logger,
            IOptionsMonitor<SystemParameter> options,
            ILifetimeScope lifetimeScope,
            IHubContext<ReportHub> hubContext,
            IMapper mapper)
        {
            this.logger = logger;
            this.options = options;
            this.lifetimeScope = lifetimeScope;
            _hubContext = hubContext;
            this.mapper = mapper;
            //   this.sqlSugarClient = sqlSugarClient;
        }


        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {

            #region 
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                   
                }
                catch (Exception ex)
                {

                    logger.LogError($"**************Assign job occur error.Error msg is:{ex.Message},stacker is:{ex.StackTrace}************");
                }
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

            #endregion



        }



        public List<Job> Filter(FilterContext filterContext, List<Job> pendingJobs)
        {
            var builder = new JobFilterBuilder();

            ////区域筛选
            //builder.Use(filterDelegate =>
            //{
            //    return (context) =>
            //    {
            //        if (context.Parameter.IsRegionCheck)
            //        {
            //            context.Jobs = context.Jobs.Where(x => x.Region == context.GetRobot().Region).ToList();
            //            return filterDelegate(context);
            //        }
            //        else
            //        {
            //            return filterDelegate(context);
            //        }
            //    };
            //});

            //接任务首次筛选
            builder.Use(filterDelegate =>
            {
                return (context) =>
                {

                    //if (string.IsNullOrEmpty(context.GetRobot().Content))
                    //    context.Jobs = context.Jobs.Where(x => x.Action == OperationType.Get).ToList();
                    //else
                    //    context.Jobs = context.Jobs.Where(x => x.Content == context.GetRobot().Content && x.Action == OperationType.Put).ToList();
                    return filterDelegate(context);
                };
            });




            var filter = builder.Build();
            filter.Invoke(filterContext);
            return filterContext.Jobs;

        }


    }
}
